package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseCamera;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.BaseCameraService;
import com.workcell.workdata.xthings.service.ManufacturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/basecamera" )
@Api(value = "basecamera", tags = "摄像头型号管理")
public class BaseCameraController {

	private final BaseCameraService baseCameraService;
	private final ManufacturerService manufacturerService;

	/**
	 * 模糊分页查询
	 * @param baseVo
	 * @return
	 */
	@ApiOperation(value = "模拟分页查询", notes = "模拟分页查询")
	@PostMapping("/vaguePage" )
	public R vaguePage(@RequestBody BaseVo baseVo) {
		return R.ok(baseCameraService.vaguePage(baseVo));
	}

	/**
	 * 通过id查询
	 * @param  id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}" )
	public R getById(@PathVariable("id" ) Long id) {
		return R.ok(baseCameraService.getById(id));
	}

	/**
	 * 新增
	 * @param baseCamera
	 * @return R
	 */
	@ApiOperation(value = "新增功能", notes = "新增")
	@PostMapping
	public R save(@RequestBody BaseCamera baseCamera) {
		Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseCamera.getDeviceManufacturer());
		Assert.notNull(manufacturer, "厂商查询为空");
		baseCamera.setManufacturerShortName(manufacturer.getManufacturerShortName());
		baseCamera.setManufacturerFullName(manufacturer.getManufacturerFullName());
		baseCamera.setManufacturer(manufacturer.getManufacturerFullName());
		baseCamera.setManufacturerCode(manufacturer.getManufacturerCode());
		return R.ok(baseCameraService.save(baseCamera));
	}

	/**
	 * 修改摄像头型号基本信息
	 * @param baseCamera
	 * @return R
	 */
	@ApiOperation(value = "修改摄像头型号", notes = "修改摄像头型号")
	@PutMapping
	public R updateBaseCamera(@RequestBody BaseCamera baseCamera) {
		Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseCamera.getDeviceManufacturer());
		Assert.notNull(manufacturer, "厂商查询为空");
		baseCamera.setManufacturerShortName(manufacturer.getManufacturerShortName());
		baseCamera.setManufacturerFullName(manufacturer.getManufacturerFullName());
		baseCamera.setManufacturer(manufacturer.getManufacturerFullName());
		baseCamera.setManufacturerCode(manufacturer.getManufacturerCode());
		return R.ok(baseCameraService.updateBaseCamera(baseCamera));
	}

	/**
	 * 修改状态
	 * @param baseCamera
	 * @return R
	 */
	@ApiOperation(value = "修改状态", notes = "修改状态")
	@PutMapping("/updateStateById")
	public R updateStateById(@RequestBody BaseCamera baseCamera) {
		return R.ok(baseCameraService.updateStateById(baseCamera));
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除", notes = "通过id删除")
	@DeleteMapping("/{id}" )
	public R delById(@PathVariable Long id) {
		return R.ok(baseCameraService.delById(id));
	}

	/**
	 * 导出excel 表格
	 * @param baseCamera 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	public List<BaseCamera> export(BaseCamera baseCamera) {
		return baseCameraService.list(Wrappers.query(baseCamera));
	}

}
